#include "InetAddress.h"
#include <sstream>

InetAddress::InetAddress(const uint16_t port) {
    bzero(&m_addr, sizeof(m_addr));
    m_addr.sin_family = AF_INET;
    m_addr.sin_port = htons(port);
    m_addr.sin_addr.s_addr = htonl(INADDR_ANY);
}

InetAddress::InetAddress(const std::string ip, const uint16_t port) {
    bzero(&m_addr, sizeof(m_addr));
    m_addr.sin_family = AF_INET;
    m_addr.sin_port = htons(port);
    m_addr.sin_addr.s_addr = inet_addr(ip.c_str());
}    
std::string InetAddress::toIp() const {
    char ip[64]{0};
    inet_ntop(AF_INET, &m_addr.sin_addr, ip, sizeof(ip));
    return ip;
}
uint16_t InetAddress::toPort() const {
    return ntohs(m_addr.sin_port);
}
std::string InetAddress::toIpPort() const {
    std::stringstream ss;
    ss << toIp() << ":" << toPort();
    
    return ss.str();
}